// pages/yuyue/yuyue.js
var app = getApp();

var appData = app.globalData;
var common = require("../../utils/Conmon.js");


Page({

  /**
   * 页面的初始数据
   */
  data: {
    flag1: false,
    pageIndex: 1,
    pageSize: 10,
    list: [],
    _num: 1,  //1销量 2价格
    flag2: true,
    pageNum: 0,
    keyword: "",
    catalogId: 0,
    HeadImg: "",
    UserName: "",
    ShareUserID: 0,
    Sharepermissions: 0,
    textmodel: true,
    textmodelTitle:true,
    shareTitle:"分享链推广",
    HideShare:true,
    UserID:0,
    KeyWord:"",
    catalogId:0

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    common.pageInit( function () {
    });
    var shareUserID = 0;
    if (options.ShareUserID > 0) {
      shareUserID = options.ShareUserID;
    }

  

    this.setData({
      keyword: options.keyword,
      catalogId: options.CatalogId,
      ShareUserID: shareUserID,
      UserID: wx.getStorageSync('UserInfo').UserID
    });
    this.bindUserInfo();
    this.checkpermissions();
    loadProduct(this, 0);
   
  },
  textModel: function () {
    this.setData({
      textmodel: !this.data.textmodel
    })

  },
  bindshareInput: function (e) {
    this.setData({
      shareTitle: e.detail.value
    })
  },
  bindUserInfo: function () {
    var that = this;
    var uid = that.data.ShareUserID;
    if(uid<=0)
    {
      uid = that.data.UserID;
    }
    wx.request({
      url: appData.apiurl + "member/api/MemberCenter/Personal",
      method: "GET",
      data: { userId: uid, custId: appData.custID },
      header: {
        "content-type": "application/json",
        'X_ManuName': appData.manuName
      },
      success: function (res) {
        wx.hideLoading()
        if (res.statusCode == 200 && res.data.code == 1200) {
          that.setData({
            HeadImg: res.data.content.HeadImg,
            UserName: res.data.content.UserName

          });
        }
      },
      fail: function (res) {
        wx.hideLoading()
        console.log(res.data)
      }
    })
  },
  checkpermissions: function () {
    var that = this;
    wx.request({
      url: appData.apiurl + "mobile/api/Sharechain/MemberRole",
      method: "GET",
      data: { userID: that.data.UserID, custID: appData.custID },
      header: {
        "content-type": "application/json",
        'X_ManuName': appData.manuName
      },
      success: function (res) {
        if (res.data.content != null && res.data.content.MemberRoleId == 1) {
          that.data.Sharepermissions = 1;
          that.setData({
            textmodelTitle: false,
            HideShare:false
          })
        
        }else{
         
        }
       
        var shareUserID = parseInt(that.data.ShareUserID);
      
        if (shareUserID == 0 && that.data.Sharepermissions == 0) {
          wx.showModal({
            content: "您不是代理商，没有主动分享权限!",
            showCancel: false,
            confirmText: "确定"
          });
        
          setTimeout(function () {
            wx.navigateBack({
              delta: 1
            })

          }, 2000);

        }


      },
      fail: function (res) {

      }
    });
  },
  
  onShareAppMessage: function (res) {
    var that = this;
    var linkPath = "pages/shareAgent/index?ShareUserID=" + that.data.UserID; + "&scene=UserID" + that.data.UserID;;
  

    return {
      title: that.data.shareTitle,
      path: linkPath,
      imageUrl: "",
      success: function (res) {
        // 转发成功
      },
      fail: function (res) {
        // 转发失败
      }
    }
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
    var pindex = this.data.pageIndex;
    var pnum = this.data.pageNum;
    if (pindex < pnum) {
      this.setData({
        pageIndex: pindex + 1
      })
      loadProduct(this, 1);
    }
    wx.stopPullDownRefresh();
  }
})



function loadProduct(that, typeid) {  //typeid  0重新加载，1分页加载

  wx.showToast({
    title: '加载中...',
    icon: 'loading'
  })
  wx.request({
    url: app.globalData.apiurl + '/mobile/api/Product/SearchProductInfo',
    data: getData(that),
    method: 'POST',
    header: {
      'X_ManuName': app.globalData.manuName
    },
    success: function (res) {
      wx.hideToast();
      if (res.data.code == 1200) {
        var list = res.data.content.Data;
        if (typeid == 1) {
          that.setData({
            list: that.data.list.concat(list),
            pageNum: res.data.content.PageNum
          });
        } else {
          that.setData({
            list: list,
            pageNum: res.data.content.PageNum
          });
        }
      }
    }
  })
}

function getData(that) {
  var data = { PageIndex: that.data.pageIndex, PageSize: that.data.pageSize, Status: -1, CustID: app.globalData.custID, KeyWord: that.data.keyword };
  if (that.data.catalogId != 0) {
    data.ProductCatalog = that.data.catalogId;
  }
  if (that.data._num == 1) {//判断排序 销量
    data.OrderBy = 1;
  } else {
    data.OrderBy = 2;
  }
  if (!that.data.flag2) {
    data.OrderByType = 0; //升序
  } else {
    data.OrderByType = 1; //降序
  }
  return data;
}